﻿using System.Reflection;
using Abp.Localization.Dictionaries;
using Abp.Localization.Dictionaries.Xml;
using Abp.Modules;
using Abp.Zero;
using Abp.Zero.Configuration;
using FutureApp.Authorization;
using FutureApp.Authorization.Roles;
using FutureApp.MultiTenancy;
using FutureApp.Users;
using Abp.MultiTenancy;

namespace FutureApp
{
    /// <Module/>
    [DependsOn(typeof(AbpZeroCoreModule))]
    public class FutureAppCoreModule : AbpModule
    {
        /// <PreInitialize/>
        public override void PreInitialize()
        {
            Configuration.Auditing.IsEnabledForAnonymousUsers = true;

            //Declare entity types
            Configuration.Modules.Zero().EntityTypes.Tenant = typeof(Tenant);
            Configuration.Modules.Zero().EntityTypes.Role = typeof(Role);
            Configuration.Modules.Zero().EntityTypes.User = typeof(User);

            //Remove the following line to disable multi-tenancy.
            Configuration.MultiTenancy.IsEnabled = true;

            //Add / remove localization sources here
            Configuration.Localization.Sources.Add(
                new DictionaryBasedLocalizationSource(
                    FutureAppConsts.LocalizationSourceName,
                    new XmlEmbeddedFileLocalizationDictionaryProvider(Assembly.GetExecutingAssembly(), "FutureApp.Localization.Source")
                )
            );

            Configuration.Modules.Zero().RoleManagement.StaticRoles.AddRange(
                new[]{
                    new StaticRoleDefinition(FutureAppConsts.StaticRole.Host.Admin, MultiTenancySides.Host),
                    new StaticRoleDefinition(FutureAppConsts.StaticRole.Tenants.Admin, MultiTenancySides.Tenant)
                }
            );
        }

        /// <Initialize/>
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }
    }
}
